package dk.aiszone.lang.messer.autosource;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dk.aiszone.lang.autosource.generatorobjects.interfaces.GeneratorObject;
import dk.aiszone.lang.autosource.generatorobjects.interfaces.SimpleBlockObject;
import dk.aiszone.lang.messer.autosource.interfaces.ComplexStatement;

/**
 * The <code>BlockStatement</code> is a container for more <code>SimpleBlocObjects</code>.
 * 
 * @author Ais
 */
public class BlockStatement extends GeneratorObject implements SimpleBlockObject {

    private final List<SimpleBlockObject> contains;

    /**
     * Constructor for the <code>BlockStatement</code>.
     * 
     * @param contains
     *            <code>List</code> of contained <code>SimpleBlockObject</code> objects.
     */
    public BlockStatement(List<SimpleBlockObject> contains) {
        this.contains = contains;
    }

    @Override
    public Object clone() {
        List<SimpleBlockObject> clones = new ArrayList<SimpleBlockObject>(contains.size());
        for (Iterator<SimpleBlockObject> iterator = contains.iterator(); iterator.hasNext();) {
            clones.add((SimpleBlockObject)iterator.next().clone());
        }

        return new BlockStatement(clones);
    }

    @Override
    public SimpleBlockObject getChild(int index) {
        return contains.get(index);
    }

    @Override
    public int getChildCount() {
        return contains.size();
    }

    @Override
    public String toJava() {
        StringBuilder sb = new StringBuilder();
        for (Iterator<SimpleBlockObject> iterator = contains.iterator(); iterator.hasNext();) {
            SimpleBlockObject object = iterator.next();
            sb.append(object.toJava());
            if (!(object instanceof ComplexStatement)) {
                sb.append(";");
            }
            sb.append("\n");
        }

        return sb.toString();
    }
}